package com.cy.server.service.impl;

import com.cy.server.dto.DepartmentDto;
import com.cy.server.pojo.Department;
import com.cy.server.mapper.DepartmentMapper;
import com.cy.server.pojo.RespBean;
import com.cy.server.service.IDepartmentService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author LiShaoZhuo
 * @since 2021-04-26
 */
@Service
public class DepartmentServiceImpl extends ServiceImpl<DepartmentMapper, Department> implements IDepartmentService {

    @Autowired
    DepartmentMapper departmentMapper;

    /**
     * @Description: 部门信息分页
     * @Param: departmentDto
     * @return:
     * @Author: LiShaoZhuo
     * @Date: 2021/4/30
     */
    @Override
    public PageInfo<Department> selectAllDepartments(DepartmentDto departmentDto) {
        PageHelper.startPage(departmentDto.getCurrentPage(), departmentDto.getPageSize());
        List<Department> list = departmentMapper.selectAllDepartments(departmentDto);
        PageInfo<Department> pageInfo = new PageInfo<>(list);
        return pageInfo;
    }

    /**
     * @Description: 新增部门信息
     * @Param:
     * @return:
     * @Author: LiShaoZhuo
     * @Date: 2021/4/30
     */
    @Override
    public RespBean addDepartment(Department department) {
        department.setEnabled(true);
        departmentMapper.addDepartment(department);
        if (department.getResult() == 1){
            return RespBean.success("调用成功",department);
        }
        return RespBean.error("调用失败");
    }

    /**
     * @Description: 删除部门信息
     * @Param:
     * @return:
     * @Author: LiShaoZhuo
     * @Date: 2021/4/30
     */
    @Override
    public RespBean deleteDepartment(Integer id) {
        Department department = new Department();
        department.setId(id);
        departmentMapper.deleteDepartment(department);
        if (-2 == department.getResult()){
            return RespBean.error("该部门下有子部门，删除失败！");
        }
        if (-1 == department.getResult()){
            return RespBean.error("该部门下有员工，删除失败！");
        }
        if (1 == department.getResult()){
            return RespBean.success("删除成功！");
        }
        return RespBean.error("删除失败！");
    }

    /**
     * @Description: 查询所有部门信息
     * @Param:
     * @return:
     * @Author: LiShaoZhuo
     * @Date: 2021/5/6
     */
    @Override
    public List<Department> getAllDepartments() {
        return departmentMapper.getAllDepartments(-1);
    }
}
